﻿   var keyNumber=0;
  Store= extend( function Store(context,x,y,width,height,forword){

     	this.init=function (){

		  	Store.prototype.init.apply(this,arguments)
			  this.lineWidth=1;
		    this.cxt=context;
		    this.x1=x-width/2;
			  this.y1=y-height/2;
		    this.x2=x+width/2;
			  this.y2=y+height/2;
			  this.x=x;
			  this.y=y;

  			this.rectangle1=new Rectangle(context,this.x1,this.y1,this.x2,this.y2);

     		this.graphs.push(this.rectangle1);

     	}
        this.init();

    },ComplexGraph)

  CarrierChain =   extend(function CarrierChain(id,context,x,y,length,rotateAngle,speed,color,distance){
     this.init=function (){
      	height=10;
      	
      	this.id=id;
  			CarrierChain.prototype.init.apply(this,arguments)
				this.cxt=context;
				this.length=length;
				this.distance=distance|1;
			
				
				this.rotateAngle=rotateAngle||	0;
				this.gap=20;
				this.speed=speed|0;
				this.x=x;
				this.y=y;
				color=color||'rgba(149,175,181,0.3)'
				this.posRectangle=new   Rectangle2(context,x,y,length,height,rotateAngle,"rgba(149,175,181,0)",color);
					this.graphs.push(this.posRectangle);
   			}
		this.IsPointInMatrix=function (p){
			
			return this.posRectangle.IsPointInMatrix(p);
			
		}
   			

	 this.draw=function(){
        CarrierChain.prototype.draw.apply(this,arguments)
      	var oldstrokeStyle =context.strokeStyle
		    var oldFont = context.font;
		    var oldfillStyle = context.fillStyle;
		    context.strokeStyle= 'rgba(0,0,0,0.1)'
		    context.font="bold 12px 宋体";
		    context.fillStyle='#ffffff'
				if(typeof(getKey) != "undefined"&&getKey("2")){//2
					 context.fillText(id%100,this.x-10,this.y+4);
				}else if(typeof(getKey) != "undefined"&&getKey("4")){//0
					context.fillText(id,this.x-15,this.y+4);
				}else if(typeof(getKey) == "undefined"){
							context.fillText(id,this.x-15,this.y+4);
				}
  		 

		    context.strokeStyle= oldstrokeStyle
		    context.font=oldFont;
		    context.fillStyle=oldfillStyle;
		 }

   	 this.init();
   } ,ComplexGraph);

   Yzj=  extend( function Yzj(id,context,x,y,width,height){
          this.init=	function (){
          this.id=id
  				Yzj.prototype.init.apply(this,arguments)
          this.width=width|1
     	  	this.x=x;
				  this.y=y;
        	this. x1= x-width/2;
		  		this. x2= x+width/2;
		  		this. y1=y-height/2;
		  		this. y2=y+height/2;
		  		this.cxt=context;
		  		/*
		  		this.graphs.push(new Line(
		  				{
		  	     			context:this.cxt,
		  	     			x1:this.x1,
		  	     			y1:this.y1,
		  	     			x2:this.x2,
		  	     			y2:this.y1

		  	     		}));
   		     this.graphs.push(new Line({
  	     			context:this.cxt,
  	     			x1:this.x1,
  	     			y1:this.y2,
  	     			x2:this.x2,
  	     			y2:this.y2

  	     		}));
        		this.graphs.push(new Line({
        					context:this.cxt,
		  	     			x1:this.x1+2,
		  	     			y1:this.y1,
		  	     			x2:this.x1+2,
		  	     			y2:this.y2}));*/
		  	     					var strokeStyle='#15659c'
		  	    this.graphs.push(new   Rectangle(context,this.x1,this.y1,this.x2,this.y2,1,strokeStyle,'rgba(149,175,181,0.5)'));



        }
        this.draw=function(){
						 Yzj.prototype.draw.apply(this,arguments)

						 		 // context.fillText(id/100,this.x-3,this.y+4);

		      	var oldstrokeStyle =context.strokeStyle
				    var oldFont = context.font;
				    var oldfillStyle = context.fillStyle;
				    context.strokeStyle= 'rgba(0,0,0,0.1)'
    		    context.font="bold 12px 宋体";
    		    context.fillStyle='#ffffff'

      		  //context.fillText(parseInt(id%100),this.x-10,this.y+4);

    		    context.strokeStyle= oldstrokeStyle
    		    context.font=oldFont;
    		    context.fillStyle=oldfillStyle;
  			 }
         this.init();
      },ComplexGraph);
   Crane=  extend(  function Crane(context,stockSize,start,end){
					  this.init=function (){
					
					      	Crane.prototype.init.apply(this,arguments)
					      	var x=(start.x+end.x)/2
	  	            var y=(start.y+end.y)/2
					        this.dx=0;
					         this.dy=0;
			       	    stockSize=(stockSize||17)*1.2
			       	    this.width=stockSize;
				      		this.len = 43*stockSize/17;
				      		this.x1=x-this.len/2;
				      		this.y1=y-this.width/2;
				      		this.x2=x+this.len/2;
				      		this.y2=y+this.width/2;
					        this.x=x;
                  this.y=y;
                  this.xCurrent=0
                  this.xGoal=0;
                  this.start=start;
                  this.end=end;
					    }
					    
					    this.setGoal=function(p){
					    	
					    		this.xGoal=p*(this.end.x-this.start.x)+this.start.x;//目标位置
					    		this.yGoal=p*(this.end.y-this.start.y)+this.start.y;//目标位置
        		      this.dx=	(this.xGoal-this.x)/90;
        		      this.dy=	(this.yGoal-this.y)/90;
					    }

					   this.draw=function(){
				     	 if(Math.abs(this.xGoal-this.x)<2){
				     	 	   this.setAnchor({x:this.xGoal,y:this.yGoal})
				     	 }else{
				     	 		 this.move(this.dx,this.dy)
				     	 }
					    	
					    	
                context.save();
                if(this.rotateAngle){
                   context.translate(this.x, this.y);
                   context.rotate(-this.rotateAngle );//旋转47�??
                   context.translate(-this.x, -this.y);
               }
               Crane.prototype.draw.apply(this,arguments)
               context.drawImage(cranePic,this.x-this.len/2,this.y-this.width/2,this.len,this.width)
               context.restore();
  			 			}

					     this.init();
      },ComplexGraph);

      CraneWay=  extend(  function CraneWay(context,x,y,x1,y1){
        this.init=function (){

              Crane.prototype.init.apply(this,arguments)
              this.xv=0//水平速度
               this.xa=0//水平加速度
               stockSize=(stockSize||17)*1.2
               this.width=stockSize;
              this.len = 43*stockSize/17;
              this.x1=x-this.len/2;
              this.y1=y-this.width/2;
              this.x2=x+this.len/2;
              this.y2=y+this.width/2;
              this.x=x;
               this.y=y;

          }

          this.draw=function(){
            Crane.prototype.draw.apply(this,arguments)
            context.drawImage(cranePic,this.x-this.len/2,this.y-this.width/2,this.len,this.width)
           }

           this.init();
  },ComplexGraph);

    Product= extend( function (context,x,y,size){
     this.init=function (){

			  Product.prototype.init.apply(this,arguments)
		      this.width=parseInt(size)||16;
		      this.len=parseInt(size)||16;
		      this.x1=x-this.width/2;
		      this.y1=y-this.len/2;
		      this.x2=x+this.width/2;
		      this.y2=y+this.len/2;
		      this.x=x;
   			  this.y=y;
		      this.graphs.push(new   Rectangle(context,this.x1,this.y1,this.x2,this.y2,1,"#ff00ff","#ff00ff"));
     		 }
        this.draw=function(){
          context.save();
          if(this.rotateAngle){
             context.translate(this.x, this.y);
             context.rotate(-this.rotateAngle );//旋转47�??
             context.translate(-this.x, -this.y);
         }

         context.drawImage(productPic,this.x-this.len/2+1,this.y-this.width/2+1,this.len-2,this.width-2)
         context.restore();
         }
/*         this.draw=function(){
						  //  Product.prototype.draw.apply(this,arguments)
		            context.drawImage(productPic,this.x-this.len/2+1,this.y-this.width/2+1,this.len-2,this.width-2)
        } */
        this.init();


      },ComplexGraph);


    StockProduct= extend( function (context,color){
     this.init=function (){

			  StockProduct.prototype.init.apply(this,arguments)
		      this.width=10;
		      var x=y=0
		      this.len=6;
		      this.x1=x-this.width/2;
		      this.y1=y-this.len/2;
		      this.x2=x+this.width/2;
		      this.y2=y+this.len/2;
		      this.x=x;
   			  this.y=y;

		      this.graphs.push(new   Rectangle(context,this.x1,this.y1,this.x2,this.y2,1,color,color));
     		 }
       this.init();
      },ComplexGraph);


  StoreRoom=  extend( function (context,config ){
  	     config=config||{}
  	     
           
 
			   this.init=function (){
			     	StoreRoom.prototype.init.apply(this,arguments)
			     	this.stores={};
			      this.x1=config.x1;
				     this.y1=config.y1;
				    var x2=config.x2;
				    var y2=config.y2;
	     			this.x=(this.x1+x2)/2;
	   			  this.y=(this.y1+y2)/2;
	   			  var width=x2-this.x1;
	   			  var height=y2-this.y1;
	   			  this.cols=config.cols||52
            this.storeWidth=width/this.cols;
            this.beginShelf=config.beginShelf||0;
            this.shelfs=config.shelfs||4
            this.shelfHigth=parseInt(height/this.shelfs);
  			    this.rows=config.rows||11
			      this.storeHigth= this.shelfHigth/(this.rows+1);
				    var wordX=this.x1+this.cols*this.storeWidth/2
				    for(var shelfId=1;shelfId<=  this.shelfs;shelfId++){
				    	  var vallShelfkey=this.beginShelf+ shelfId
					    	var keyShelfId =vallShelfkey*10000;
					    	for (var col=1;col<= this.cols;col++){
						    		var keyRow=keyShelfId+col*100;
					          for(var row=1;row<= this.rows;row++){
					           	var key =keyRow+row;
					           	var tmpx=this.x1+col*this.storeWidth-this.storeWidth/2;
					           	var tmpy=this.y1+(this.rows+1-row)*this.storeHigth+(shelfId-1)*this.shelfHigth;
					          	var store=new Store(context,tmpx,tmpy,this.storeWidth,this.storeHigth);
					            this.graphs.push(store);
					            this.stores['k'+key]=store;
					          }
				        }
				        var wordY=this.y1+(shelfId)*this.shelfHigth-this.storeHigth
				        var cfg={context:context,word:vallShelfkey,x:wordX,y:wordY,font:'bold '+(this.shelfHigth)+'px 宋体',fillStyle:'rgba(0,0,0,0.15)'}
				        var word=new Word(cfg);
					      this.graphs.push(word);
				    }


			   }
			   this.init();

			  this.getId =function (x,y){
				   	var col =  x-this.x1;
				   	col = col/this.storeWidth+1
				    col=	Math.round(col)
				   	if(col<1){
				   		return 0;
				   		}
				   	shelf = y-this.y1
				   	shelfId = shelf/this.shelfHigth+1;
				   	shelfId=parseInt(shelfId)
				   	if(shelfId<1){
				   		return 0;
				   		}
				   	row= this.rows-(shelf%this.shelfHigth)/this.storeHigth
				   	row=Math.round(row);
				   	if(row<1||row> this.rows){
				   		return 0;
				   	}
				   shelfId+=	this.beginShelf
			   	 return shelfId*10000+col*100+row;
			   	}


      } ,ComplexGraph);





BarGraph=  extend( function BarGraph (cfg){

			 this.init=function (){

			     	BarGraph.prototype.init.apply(this,arguments)
			     	this.xName=cfg.xName;
			     	this.yName=cfg.yName;
			     	this.title=cfg.title;
	          var  context=cfg.context;
	          this.cxt=context;
	          this.realheight = cfg.y2-cfg.y1;
	          this.realwidth=cfg.x2-cfg.x1;

            var data = cfg.data;



            var grid_cols = data.length + 1;
            var grid_rows = 4;
            this. cell_height = this.realheight / grid_rows;
            this. cell_width = this.realwidth / grid_cols;



					 	this.graphs.push(new Word({//标题
							 	x:(cfg.x2+cfg.x1)/2-40,
							 	y:cfg.y1+20,
					 			context:cfg.context,
					 			word:this.title,
					 			font:'30px sans-serif',
					 			strokeStyle : "black"
					 	}));

					 	this.graphs.push(new BrokenLine({//边框线
							 	x:this.x,
							 	y:this.y,
					 			context:cfg.context,
					 			points:[{x:cfg.x1,y:cfg.y1},{x:cfg.x1,y:cfg.y2},{x:cfg.x2,y:cfg.y2}],
					 			strokeStyle : "black"
					 	}));

		      	this.graphs.push(new Word({//
									 	x:cfg.x1,
									 	y:cfg.y1,
							 			context:cfg.context,
							 			word:this.yName,
							 			strokeStyle : "black"
					  }));
						this.graphs.push(new Word({
									 	x:cfg.x2,
									 	y:cfg.y2+12,
							 			context:cfg.context,
							 			word:this.xName,
							 			strokeStyle : "black"
						}));

				      this.reflashData(data);


			     }



			     this.reflashData=function(data){
					     this.data=data;
					     var max_v = getMax(data);

				      // 将数据换算为坐标
				      var points = [];
				      for( var i=0; i < data.length; i++){
				        var v= data[i].value;
				        var px = this.cell_width *(i +1);
				        var py = this.realheight - this.realheight*(v / max_v);

				        points.push({"x":px,"y":py});
				      }

				      var newPraphs=[];
				      for(var i in this.graphs){
					      	var g=this.graphs[i];
						      if(g.config.method!='reflashData'){
						      	newPraphs.push(g);
						      }

				      }

				      //绘制坐标图形
				      for(var i in points){
				          var p = points[i];
				      		newPraphs.push(new Rect({
				      							method:'reflashData',
													 	x:p.x+cfg.x1,
													 	y:p.y+cfg.y1,
											 			context:this.cxt,
											 			width:15,
											 			height : this.realheight-p.y,
											 			fillStyle:"green"
									}));
				        	newPraphs.push(new Word({
				        						method:'reflashData',
													 	x: p.x+cfg.x1,
													 	y: p.y - 15+cfg.y1,
											 			context:this.cxt,
											 			word:data[i].value,
											 			strokeStyle : "black"
										}));

										newPraphs.push(new Word({//水平名称
														method:'reflashData',
													 	x: p.x+cfg.x1,
													 	y:this.realheight+12+cfg.y1,
											 			context:this.cxt,
											 			word:data[i].lab,
											 			strokeStyle : "black"
										}));
				      }

			     		this.graphs= newPraphs;
			     }

			 	  function  getMax(data){
						   var max_v =0;
					      for(var i = 0; i<data.length; i++){
					        if (data[i].value > max_v) { max_v =data[i].value};
					      }
					      max_v = max_v * 1.1;
					      return max_v;
					}
					 this.init(cfg);
} ,ComplexGraph);









  LineChart=  extend( function LineChart (cfg){

			     this.init=function (){
			     			this.x1=cfg.x1;
					    	this.y1=cfg.y1;
					    	this.x2=cfg.x2;
					    	this.y2=cfg.y2;
					      this.cxt=cfg.context;
					      this.data=cfg.data;
					      this.title=cfg.title
					      this.left=10;
      					this. buttom=10;
					      this.graphs.push(new Word({//标题
									 	x:(this.x1+this.x2)/2-100,
									 	y:this.y1+30,
							 			context:cfg.context,
							 			word:this.title,
							 			font:'30px sans-serif',
							 			strokeStyle : "black"
							 	}));


				        	this.graphs.push(new BrokenLine({
										 	x: this.x1,
										 	y:this.y1,
								 			context:this.cxt,
								 			points:[{x:this.x1,y:this.y1},{x:this.x1,y:this.y2-this.buttom},{x:this.x2,y:this.y2-this.buttom}],
								 			strokeStyle : "black"
								}));


									this.graphs.push(new Word({
												 	x: this.x1,
												 	y:this.y1,
										 			context:this.cxt,
										 			word:'Yindex',
										 			strokeStyle : "black"
									}));

									this.graphs.push(new Word({
												 	x: this.x2,
												 	y:this.y2,
										 			context:this.cxt,
										 			word:'Xindex',
										 			strokeStyle : "black"
									}));

			     			this.reflashData(this.data)
			     }
			      this.reflashData=function(data){
			      			this.data=data;
			       			var points= getPoint(this.data,this.x1,this.y1,this.x2,this.y2)
					        var newPraphs=[];
						      for(var i in this.graphs){
							      	var g=this.graphs[i];
								      if(g.config.method!='reflashData'){
								      	newPraphs.push(g);
								      }

						      }

				      	newPraphs.push(new BrokenLine({
				      				method:'reflashData',
										 	x: this.x1,
										 	y:this.y1,
								 			context:this.cxt,
								 			points:points,
								 			strokeStyle : "black"
								}));
					      var len = this.data.length;
				        for(var i=0; i<len; i++){
				        			var p=points[i]
				        			newPraphs.push(new Word({
				        						method:'reflashData',
													 	x: p.x,
													 	y: p.y-10,
											 			context:this.cxt,
											 			word:this.data[i].y,
											 			strokeStyle : "black"
										  }));
				        	   newPraphs.push(new Word({
				        	   				 method:'reflashData',
													 	x: p.x,
													 	y: this.y2,
											 			context:this.cxt,
											 			word:this.data[i].x,
											 			strokeStyle : "black"
										  }));
										  newPraphs.push(new Point({
										      	method:'reflashData',
													 	x: p.x,
													 	y: p.y,
											 			context:this.cxt,
											 			strokeStyle : "black"
										  }));

				        }
				        		this.graphs= newPraphs;
			      }

			     function getPoint(dict,x1,y1,x2,y2){
								var points=[];

								var rang=getRange(dict)

								var k = (y2-y1-100)/(rang.max-rang.min);


								var len = dict.length;
				 				var s=(x2-x1)/(len);
				        var xArr = [];
				        for(var i=0; i<len; i++){
						        var d=dict[i];
						    		points.push({x:i * s+s/2+x1,y:y2-((d.y-rang.min)*k + 50)})

				        }
						    return  points;
						}

			      function getRange(data){
			     			var tmp_minY= tmp_maxY=data[0].y
			     		  var len = data.length;
					      for(var i=1; i<len; i++){
							      var d=data[i];
							      if(d.y>tmp_maxY){
							      	tmp_maxY=d.y;
							      }
							      if(d.y<tmp_minY){
							       	tmp_minY=d.y;
							      }
					      }
						    return {max:tmp_maxY,min:tmp_minY} ;
			     }

			     this.init(cfg);
      } ,ComplexGraph);















